package com.carkeeper.user.controller;

import com.carkeeper.entity.Result;
import com.carkeeper.entity.StatusCode;
import com.carkeeper.pojo.Admin;
import com.carkeeper.user.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @GetMapping
    public Result findAll(){

        List<Admin> adminList = adminService.findAll();

        return new Result(true,StatusCode.OK,"查询成功",adminList);
    }

    @PostMapping
    public Result add(@RequestBody Admin admin){

        adminService.add(admin);

        return new Result(true,StatusCode.OK,"添加成功");
    }

    @PutMapping("/{id}")
    public Result update(@RequestBody Admin admin,@PathVariable Integer id){

        admin.setId(id);

        adminService.update(admin);

        return new Result(true,StatusCode.OK,"修改成功");

        }
    @GetMapping("/lockBlock/{id}")
    public Result<Admin> lockBlock(@PathVariable("id") Integer id){

        Admin status = adminService.lockBlock(id);

        return new Result<>(true,StatusCode.OK,"已锁定",status);

    }

    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable("id") Integer id){

        Admin admin = adminService.findById(id);

        return new Result(true,StatusCode.OK,"查询成功",admin);
    }

    @GetMapping("/findByUsername/{username}")
    public Result findByUsername(@PathVariable("username") String username){

        Admin byUsername = adminService.findByUsername(username);

        return new Result(true,StatusCode.OK,"查询成功",byUsername);

    }
}
